package entity

import (
	"go-process/config"
	"go-process/entity/req"
	"time"
)

type User struct {
	Id         int       `gorm:"column:id;primary_key;AUTO_INCREMENT"`
	Name       string    `json:"name" validate:"required"`
	Password   string    `json:"password" validate:"required"`
	Sex        int       `json:"sex" validate:"required"`
	Age        int       `json:"age" validate:"min=1,max=120"`
	Email      string    `json:"email" validate:"required,email"`
	Phone      string    `json:"phone"`
	Address    string    `json:"address"`
	Status     int       `json:"status"`
	CreateTime time.Time `json:"create_time"`
	UpdateTime time.Time `json:"update_time"`
}

func (User) TableName() string {
	return "t_user"
}
func (u User) Insert() (int64, error) {
	tx := config.DB.Create(&u)
	if tx.Error != nil {
		return 0, tx.Error
	}
	return tx.RowsAffected, nil

}
func (u User) Update() (int64, error) {
	tx := config.DB.Save(&u)
	return tx.RowsAffected, tx.Error
}
func (u User) Delete(id int) (int64, error) {
	return config.DB.Where("id = ?", id).Delete(u).RowsAffected, config.DB.Where("id = ?", id).Delete(u).Error
}
func (u User) GetUserById(id int) (User, error) {
	var user User
	return user, config.DB.Where("id = ?", id).First(&user).Error
}
func (u User) GetUserList(UserReq req.UserPageReq) ([]User, error) {
	var users []User
	page := UserReq.PageNum
	pageSize := UserReq.PageSize
	db := config.DB.Model(&User{})
	if UserReq.UserReq.Name != "" {
		db = db.Where("name like ?", "%"+UserReq.UserReq.Name+"%")
	}
	if UserReq.UserReq.Phone != "" {
		db = db.Where("phone like ?", "%"+UserReq.UserReq.Phone+"%")
	}
	if UserReq.UserReq.Email != "" {
		db = db.Where("email like ?", "%"+UserReq.UserReq.Email+"%")
	}
	if UserReq.UserReq.Address != "" {
		db = db.Where("address like ?", "%"+UserReq.UserReq.Address+"%")
	}

	if UserReq.UserReq.Status != 0 {
		db = db.Where("status = ?", UserReq.UserReq.Status)
	}
	if UserReq.UserReq.Sex != 0 {
		db = db.Where("sex = ?", UserReq.UserReq.Sex)
	}
	if UserReq.UserReq.Age > 0 {
		db = db.Where("age >= ?", UserReq.UserReq.Age)
	}
	db = db.Order("id desc").Offset((page - 1) * pageSize).Limit(pageSize)
	tx := db.Find(&users)
	return users, tx.Error
}

func (u User) GetTotal(UserReq req.UserPageReq) (int64, error) {
	total := int64(0)
	db := config.DB.Model(&User{})
	if UserReq.UserReq.Name != "" {
		db = db.Where("name like ?", "%"+UserReq.UserReq.Name+"%")
	}
	if UserReq.UserReq.Phone != "" {
		db = db.Where("phone like ?", "%"+UserReq.UserReq.Phone+"%")
	}
	if UserReq.UserReq.Email != "" {
		db = db.Where("email like ?", "%"+UserReq.UserReq.Email+"%")
	}
	if UserReq.UserReq.Address != "" {
		db = db.Where("address like ?", "%"+UserReq.UserReq.Address+"%")
	}

	if UserReq.UserReq.Status != 0 {
		db = db.Where("status = ?", UserReq.UserReq.Status)
	}
	if UserReq.UserReq.Sex != 0 {
		db = db.Where("sex = ?", UserReq.UserReq.Sex)
	}
	if UserReq.UserReq.Age > 0 {
		db = db.Where("age >= ?", UserReq.UserReq.Age)
	}
	tx := db.Count(&total)
	return total, tx.Error
}
